const multer = require('multer')
const path = require('path');

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './public/upload')
  },
  filename: function (req, file, cb) {
    const ext = path.extname(file.originalname)
    cb(null, Date.now() + ext)
  }
})

exports.beforeupload = multer({ storage: storage })  


exports.upload = async (req, res, next) => {
  try {
    req.file.path = '/' + req.file.path
    res.status(201).json(req.file)
  } catch (err) {
    next(err)
  }
}